import ProjectModel from "../../schema/project";
import type Typings from "../../typings";

// Mongoose 类的实例
import MongooseController from "../mongoose/mongoose";
const ProjectIns = new MongooseController(ProjectModel);

// response 响应类型类的实例
import ResController from "../res/res";
const ResIns = new ResController();

class ProjectController {
  constructor() {}

  // 递归查询项目信息
  static async recursion(query: Typings.Common.plainObj = {}) {
    const data: Typings.Project.props[] = await ProjectIns.Find(query);

    for (let i = 0, len = data.length; i < len; i++) {
      const children = await ProjectController.recursion({
        parentKey: data[i].key,
      });

      data.splice(i, 1, {
        ...JSON.parse(JSON.stringify(data[i])),
        children,
      });
    }

    return data;
  }

  // 项目树
  async tree(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const data = await ProjectController.recursion({ parentKey: "" });
      ResIns.Success(res, data);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 新增
  async add(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { parentKey, title, key } = req.body || {};

      const projects: Typings.Project.props[] = await ProjectIns.Find({
        key,
      });

      if (projects.length) {
        ResIns.isExist(res);
        return;
      }

      await ProjectIns.SaveOne(req, {
        parentKey,
        title,
        key,
      });

      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 更新
  async update(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { id, parentKey, title, key } =
        <Partial<Typings.Project.props>>req.body || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      const updateContent: Partial<Typings.Project.props> = {};

      updateContent.parentKey = parentKey;
      updateContent.title = title;
      updateContent.key = key;

      await ProjectIns.UpdateOne({ id }, updateContent);
      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 查看
  async check(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { id } = req.query || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      // 查询
      const projects: Typings.Project.props[] = await ProjectIns.Find({
        id,
      });

      if (!projects.length) {
        // 不存在
        ResIns.isNotExist(res);
        return;
      }

      // 存在时
      ResIns.Success(res, projects[0]);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 删除
  async remove(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      let { id } = req.query || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      // 删除
      await ProjectIns.RemoveOne({ id });

      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }
}

export default ProjectController;
